logistic.sel.fn <- function(age, pars) 1/(1 + exp(-(-prod(pars) + pars[1]*age)))

par(mfrow = c(1,2))
plot(1:10, sel.fn(1:10, pars = c(1,5)))
plot(1:10, sel.fn(1:10, pars = c(5,2)))

source('pop.sim.fn.r')

years <- 1920:1959
source('sa.dat.fn.r')
x <- sa.dat.fn(years = years, sel.fn = logistic.sel.fn)
write.csv(x$survey.raw, file = 'survey.dat.csv')
write.csv(x$saa, file = 'saa.csv')
write.csv(x$catch.raw, file = 'catch.dat.csv')
write.csv(x$caa, file = 'caa.csv')

x <- sa.dat.fn(years = years, sel.fn = logistic.sel.fn, samp.prob.c = 0.01, samp.prob.s = 0.1)
write.csv(x$survey.raw, file = 'survey.dat.sampled.csv')
write.csv(cbind(x$survey.y, x$saa), file = 'saa.sampled.csv')
write.csv(x$catch.raw, file = 'catch.dat.sampled.csv')
write.csv(cbind(x$catch.y, x$caa), file = 'caa.sampled.csv')


write.csv(x$catches, file = 'catches.csv')
write.csv(x$surveys, file = 'survey.csv')

source('write.admb.dat.fn.r')
write.admb.dat.fn(fish_dat = x$catch.raw[which(x$catch.raw[,'cohort'] == 1920),-5], 
	survey_dat = x$survey.raw[which(x$survey.raw[,'cohort'] == 1920),-5])
write.admb.dat.fn(fish_dat = x$catch.raw[which(x$catch.raw[,'age.mid'] <40),-5], 
	survey_dat = x$survey.raw[which(x$survey.raw[,'age.mid'] <40),-5])
source('loglike.cohort.fn.r')
loglike.cohort.fn(N0=1e6-sum(x$catches[,1]) - sum(x$surveys[,1]),F = rep(0.3,12*length(years)), M = rep(0.2, 12*length(years)), q.s = 0.3, 
	E.s = rep(c(0.05,0.05,0.05,rep(0,9)),length(years)), survey.sel.pars = c(5,2), catch.sel.pars = c(1,5),
	intervals = rep(1/12,12*length(years)), F.ind = rep(1,12*length(years)), catch = x$catches[,1], survey = x$surveys[,1])
